package com.ha8.netease.views.team.publish
{
	import com.ha8.netease.AppFacade;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.views.team.publish.PublishView;
	import com.ha8.netease.views.utils.DataUtil;
	import com.ha8.netease.views.utils.ToolTipUtil;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.MouseEvent;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class PublishViewMediator extends Mediator
	{
		public function PublishViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'PublishViewMediator';
		
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			view.publishBtn.addEventListener(MouseEvent.CLICK, publishBtn_clickHandler);
		}
		
		override public function listNotificationInterests():Array
		{
			return [PrivateNotify.GET_PUBLISH_RESULT, PrivateNotify.GET_PUBLISH_ERROR];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch(notification.name)
			{
				case PrivateNotify.GET_PUBLISH_RESULT:
					ToolTipUtil.alert("发布成功!");
					view.interval = setInterval(getTiemHandler,1000);
					break;
				case PrivateNotify.GET_PUBLISH_ERROR:
					ToolTipUtil.alert("发布失败!");
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():PublishView
		{
			return viewComponent as PublishView;
		}
		
		public function publishBtn_clickHandler(e:MouseEvent):void
		{
			if (view.contentText.text == "")
			{
				ToolTipUtil.alert("你没有填写发布内容,请填写!");
			}
			else
			{
				weiboProxy.postStatus(view.contentText.text);
			}
		}
		
		private function getTiemHandler():void
		{
			clearInterval(view.interval);
			view.close();
		}
		
		public function get weiboProxy():WeiboProxy
		{
			return AppFacade.getInstance().retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
	}
}